package com.lyz.myself.controller;


import com.lyz.myself.config.utils.Result;
import com.lyz.myself.config.utils.base.CodeMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.net.HttpURLConnection;

import java.net.URL;
import java.util.Map;

@Api(description="测试post接口")
@RestController
@RequestMapping("/testPostController")
public class TestPostController {


    @ApiOperation("测试接口")
    @RequestMapping(path = "/testPost", method = RequestMethod.POST)
    public Result testPost(@RequestBody Map<String,String> map) {
        File file = new File("D:\\text\\demo.txt");
        //开关
        boolean keys = false;
        if (file.exists()) {

            file.delete();
        }

        if (!file.exists()) try {
            System.out.println("开始新建文件");
            file.createNewFile();
            OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file), "GB2312");
            BufferedWriter bw = new BufferedWriter(osw);
            bw.write(read(map.get("url")));
           keys = true;
            bw.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        if(keys){
            return Result.success(read(map.get("url")),"导入成功");
        }else{
            return Result.error(CodeMsg.ERROR,"导入失败");
        }

    }

    //创建读取接口中数据的方法
    public static String read(String str) {
        URL url = null;
        BufferedReader reader = null;
        HttpURLConnection connection = null;
        InputStreamReader ins = null;
//
        try {
            // 设置url地址
            url = new URL(str);
            System.out.println("已完成20%。。。");

            // 获取连接通道
            connection = (HttpURLConnection) url.openConnection();
            System.out.println("连接通道获取成功");
            connection.setRequestMethod("POST");//设置请求方式为POST
            connection.setDoOutput(true);//允许写出
            connection.setDoInput(true);//允许读入
            connection.setUseCaches(false);//不使用缓存
            System.out.println("已完成50%。。。");
            // 输入流
            ins = new InputStreamReader(connection.getInputStream(),"utf-8");
            System.out.println("输入流获取完毕");
            // 读取
            reader = new BufferedReader(ins);
            // 创建可变字符串
            StringBuffer sb = new StringBuffer();
            System.out.println("已完成80%。。。");
            String line;

//			readLine()方法，当读取流读取数据时使用，当读到\n、\r时，会跟着换行，
//			同时会以字符串的形式返回这一行，当读取完所有数据时，会返回null
            System.out.println("开始读取");
            while ((line = reader.readLine()) != null) {
                System.out.println("导入中。。。");
                sb.append(line + "\n");
            }
            return sb.toString();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("Error GetURL:" + e);
            System.out.println("Error GetURL:" + url);
            e.printStackTrace();
        } finally {
            if (ins != null) {
                try {
                    ins.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                connection.disconnect();
            }
        }
        return null;

    }
}
